#!/usr/bin/python
# -*- encoding: utf-8 -*-
'''
@File    :   2.py
@Time    :   2020/06/24 13:43:18
@Author  :   Malygos_l 
@Version :   1.0
@Contact :   751756061@qq.com
@WebSite :   www.gitee.com/malygos_l
'''
# Start typing your code from here
'''
2 给定一组数据网址数据，请判断这些网址是否可以访问； 用多线程的方式来实现；
   请查资料，Python的 requests库，如何判断一个网址可以访问；
'''

import threading
import requests

i = 0
mutex = threading.Lock()


def url_get(url):
    try:
        # 做一个user-agent模拟浏览器发送请求,也可以加入其它字段
        kv = {
            'User-Agent':
            'Mozilla/5.0 (Windows NT 10.0; WOW64) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER'
        }
        requests.get(url, headers=kv)
        print("%s可以访问！" % url)
    except:
        print("%s异常！" % url)


def getHtmlText(url):
    try:  # 网络连接有风险，异常处理很重要
        r = requests.get(url, timeout=5)  # 查一下这个方法的使用
        r.raise_for_status()  # 如果状态不是200，引发HTTPError异常
        r.encoding = r.apparent_encoding
        print("%s is normal!" % url)
        return r.text
    except:
        #  return "产生异常"
        print("%s has exception!" % url)


def new_datas(new_data):
    with open('homework8/url_data.txt', 'r') as f:
        for line in f.readlines():
            for x in line.split('\n'):
                #将分隔符换成space进行分行
                for symbol in ";":
                    x = x.replace(symbol, ' ')
                for i in x.split():
                    new_data.append(i)
    return new_data


def Muiltithreading():
    global i
    new_data = []
    new_datas(new_data)
    while new_data[i]:
        mutex.acquire()
        # getHtmlText(i)
        url_get(new_data[i])
        mutex.release()
        i += 1


def main():
    for i in range(200):
        threading.Thread(target=Muiltithreading).start()


if __name__ == "__main__":
    main()